<?php

namespace App\Controllers\Lessions;

use App\Models\Lession\Lession;
use App\Models\Lession\CommentCopy;
/**
    用于获取多节课的课程，专栏的评论，
 */
class GetCommentAll extends \App\Libs\Base\BaseAction
{

    public function execute()
    {
        if(!$this->request->hasQuery('course_id') || !$this->request->hasQuery('type')){
            return \App\Libs\Views\Json::view(1, '缺少参数', []);
        }
        $courseID = $this->request->getQuery('course_id');
        $type = $this->request->getQuery('type');
        $page = $this->request->getQuery('page','int',1);
        $num = $this->request->getQuery('num','int',10);
        $offset = ($page-1)*$num;
        switch($type){
            case '1':
                //获取多节课的课程的评论
                $table = 'App\Models\Lession\Course';
                break;
            case '2':
                //获取专栏的评论；
                $table = 'App\Models\Lession\Scolumn';
        }
        $course = $table::findOne([
            'conditions' => 'id=?1',
            'bind' => [1=>$courseID]
        ]);
        if(!empty($course)){
                $allLessions = Lession::findAll([
                    'conditions' => 'coustar_id=?1 and type=?2',
                    'bind' => [1=>$courseID,2=>$type]
                ]);
                $allLessions->rewind();
                if($allLessions->valid()){
                    while($allLessions->valid()){
                        $lessionIDArray[] = $allLessions->current()->id;
                        $allLessions->next();
                    }
                    $allComment = CommentCopy::findAll([
                        'conditions' => 'lession_id in ({fds:array}) and fid=0',
                        'bind' => ['fds'=> $lessionIDArray],
                        'order'=>'ctime desc',
                        'offset' => $offset,
                        'limit' => $num
                    ]);
                    $allComment->rewind();
                    if($allComment->valid()){
                        while($allComment->valid()){
                            $currentTopComment = $allComment->current();
                            $allChildComment = CommentCopy::count([
                                'conditions' => 'fid=?1',
                                'bind' => [1 => $currentTopComment->id],
                            ]);
                            $topComment = $currentTopComment->toArray(['id', 'from_uid', 'lession_id', 'content', 'fid', 'ctime']);
                            $topComment['from_username'] = $currentTopComment->getSingleUserName($topComment['from_uid']);
                            $topComment['reply_num'] = $allChildComment;
                            $allTopComment[] = $topComment;
                            $allComment->next();
                        }
                        return \App\Libs\Views\Json::view(0, '成功', $allTopComment);
                    }else{
                        return \App\Libs\Views\Json::view(1, '暂没有评论', []);
                    }
                }else{
                    return \App\Libs\Views\Json::view(1, '还没有课程,更不会有评论', []);
                }
        }else{
            return \App\Libs\Views\Json::view(1, '不存在', []);
        }
        //return \App\Libs\Views\Json::view(0, '成功', []);
    }
}
